﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCRUM.Core.BusinessObjects;
using System.Data.Common;

namespace SCRUM.Core.DataProviders
{
    /// <summary>
    /// Privider for comunication with the database
    /// </summary>
    public partial class DBDataProvider : DataProvider
    {
        /// <summary>
        /// Gets the widget rights for specified widgetType ID and role ID
        /// </summary>
        /// <param name="widgetTypeID">The widget type ID.</param>
        /// <param name="roleID">The role ID.</param>
        /// <returns></returns>
        public override IList<WidgetRight> GetWidgetRights(int widgetTypeID, int roleID)
        {
            IList<WidgetRight> widgetRights = new List<WidgetRight>();
            try
            {
                DbParameter dbWidgetTypeID = CreateParameter("widgetTypeID", widgetTypeID);
                DbParameter dbRoleID = CreateParameter("roleID", roleID);

                connection.Open();
                DbDataReader data = GetGrid("WidgetRights/GetWidgetsRights", dbWidgetTypeID, dbRoleID);

                while (data.Read())
                {
                    int wid = Convert.ToInt32(data["ID"]);
                    int wtid = Convert.ToInt32(data["WIDGET_TYPE_ID"]);
                    string wname = (string)data["WIDGET_TYPE_NAME"];
                    int rid = Convert.ToInt32(data["ROLE_ID"]);
                    string rname = (string)data["ROLE_NAME"];
                    bool read = Convert.ToBoolean(data["READ_NN"]);
                    bool write = Convert.ToBoolean(data["WRITE_NN"]);
                    bool existing = Convert.ToBoolean(data["EXISTING"]);

                    WidgetRight widgetRight = new WidgetRight(
                        wid,wtid, wname, rid, rname, read, write, existing
                    );
                    widgetRights.Add(widgetRight);
                }
            }
            finally
            {
                connection.Close();
            }
            return widgetRights;
        }

        /// <summary>
        /// Updates the write right.
        /// </summary>
        /// <param name="rightID">The right ID.</param>
        /// <param name="right">if set to <c>true</c> [right].</param>
        public override void UpdateWriteRight(int rightID, bool right)
        {
            try
            {
                DbParameter dbWrite = CreateParameter("write", right);
                DbParameter dbRightID = CreateParameter("rightID", rightID);

                connection.Open();
                ExecuteNonQuery("WidgetRights/UpdateWriteRight", dbRightID, dbWrite);
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Adds the view right for specified roleID and widgetTypeID
        /// </summary>
        /// <param name="roleID">The role ID.</param>
        /// <param name="widgetTypeID">The widget type ID.</param>
        public override void AddViewRight(int roleID, int widgetTypeID)
        {
            try
            {
                DbParameter dbWidgetTypeID = CreateParameter("widgetTypeID", widgetTypeID);
                DbParameter dbRoleID = CreateParameter("roleID", roleID);

                connection.Open();
                ExecuteNonQuery("WidgetRights/AddViewRight", dbWidgetTypeID, dbRoleID);
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Removes the view right for specified right ID
        /// </summary>
        /// <param name="rightID">The right ID.</param>
        public override void RemoveViewRight(int rightID)
        {
            try
            {
                DbParameter dbRightID = CreateParameter("rightID", rightID);

                connection.Open();
                ExecuteNonQuery("WidgetRights/RemoveViewRight", dbRightID);
            }
            finally
            {
                connection.Close();
            }
        }

        /// <summary>
        /// Gets the user right for widget.
        /// </summary>
        /// <param name="currentUserID"></param>
        /// <param name="widgetTypeID">The widget type ID.</param>
        /// <returns>Right for user for the widget</returns>
        public override UserRightForWidget GetUserRightForWidget(int currentUserID, int widgetTypeID)
        {
            try
            {
                bool see = false;
                bool write = false;

                DbParameter dbUserID = CreateParameter("userID", currentUserID);
                DbParameter dbWidgetTypeID = CreateParameter("widgetTypeID", widgetTypeID);

                connection.Open();

                DbDataReader data = GetGrid("WidgetRights/GetUserWidgetRights", dbUserID, dbWidgetTypeID);
                while (data.Read())
                {
                    see = true;
                    write = write || GetBoolByColumn(data, "CAN_WRITE");
                }

                return new UserRightForWidget(currentUserID, widgetTypeID, see, write);
            }
            finally
            {
                connection.Close();
            }
        }
    }
}
